<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<div class="sidebar-warp">
    <div class="sidebar-con" id="sidebar-con">
        <div class="sidebar-nav fl">
            <div class="msg-list-t">
                <!-- 会员中心 -->
                <div onclick="showusercenter();" class="sid-bot sid-user sid-nav-a">
                    <img src="${webctx}/images/sidebar/sid-user.png" alt="会员中心">

                    <!-- 会员中心小窗口  未登录-->
                    <div class="sid-user-msg">
                        <div class="user-msg-top" onclick="side_tologin();">
                            <a class="user-img fl" href="javascript:void(0);"><img src="${webctx}/images/uhead.jpg"></a>
                            <div class="top-text fl">
                                <p class="login-out-msg">您当前尚未登录</p>
                                <p>马上去<a class="go-login" href="javascript:void(0);">登录</a></p>
                            </div>
                            <div class="clear"></div>
                        </div>
                        <div class="user-msg-bot" onclick="side_tologin();">
                            <div class="bot-item integral">
                                <p>0</p>
                                <p>积分</p>
                            </div>
                            <div class="bot-item coupon">
                                <p>0</p>
                                <p>优惠券</p>
                            </div>
                            <div class="bot-item news">
                                <p>0</p>
                                <p>消息</p>
                            </div>
                        </div>
                    </div>
                </div>


                <a href="javascript:void(0);" class="sid-ct sid-cart sid-nav-a" id="carbox"><img
                        src="${webctx}/images/sidebar/sid-cart.png" alt="购物车" title="购物车">购物车 <span
                        class="num-msg homecount" id="cartNum">0</span></a><!-- 购物车 -->
                <a href="javascript:void(0);" class="sid-ct sid-company sid-nav-a"><img
                        src="${webctx}/images/sidebar/sid-company.png" alt="优惠券" title="优惠券">优惠券 <span id="couponNum"
                                                                                                       class="num-msg homecount">0</span></a>
                <!-- 优惠券 -->
            </div>

            <div class="msg-list-b">
                <a href="javascript:;" onclick="BaiduBusiness();"
                   class="sid-bot sid-service sid-nav-a"><img src="${webctx}/images/sidebar/sid-service.png"
                                                              alt="客服"><span
                        class="sidebar-nav-msg">联系客服</span></a><!-- 客服 -->
                <a href="${ctx}/user/stationnews" class="sid-bot sid-news sid-nav-a"><img
                        src="${webctx}/images/sidebar/sid-news.png" alt="消息"><span
                        class="sidebar-nav-msg">站内消息</span></a><!-- 消息 -->
                <a href="javascript:;" class="sid-bot sid-code sid-nav-a"><img
                        src="${webctx}/images/sidebar/sid-code.png"
                        alt="二维码" title="二维码"><span
                        class="sidebar-nav-msg-code"><img
                        src="${webctx}/images/sidebar/sid-QR.png" alt="二维码">微信商城下单更优惠</span></a><!-- 二维码 -->
                <a href="javascript:;" class="sid-bot sid-stick sid-nav-a"><img
                        src="${webctx}/images/sidebar/sid-stick.png"
                        alt="返顶" title="返顶"><span class="sidebar-nav-msg">返回顶部</span></a>
                <!-- 返顶 -->
            </div>
        </div>
        <!-- 购物车 -->
        <div class="sidebar-commodity fl">
            <div class="com-title clear">最近添加的商品 <span class="ico-close clear"></span></div>
            <div id="cart_content">
                <!-- 未登录状态 -->
                <div class="log-out" style="display: none">
                    <p>购物车中还没有商品</p>
                    <p>赶快登录查看我的商品吧</p>
                    <a href="${ctx}/user/showlogin" class="log-in-btn">登录</a>
                </div>

                <!-- 登录了 还没添加商品状态 -->
                <p class="com-null" style="display: none">您尚未添加商品至购物车，赶紧去逛逛吧~</p>
            </div>
        </div>

        <!-- 优惠券 -->
        <div class="sidebar-coupon fl">
            <div class="com-title clear">优惠券 <span class="ico-close clear"></span></div>
            <div class="coupon-con">
                <div class="coupon-list">
                    <!-- 可领取优惠券 -->
                    <div class="get-coupon">
                        <div class="coupon-tit"><span class="txt">可领取优惠券</span> <span class="bor"></span></div>

                        <!-- 空 -->
                        <div class="con-null" style="display: none">
                            <img src="${webctx}/images/con-null.png" alt="">
                            <p>暂无可领取的优惠券</p>
                        </div>

                        <div class="get-coupon-list">
                        </div>
                    </div>
                    <!-- 我的优惠券 -->
                    <div class="my-coupon">
                        <div class="coupon-tit"><span class="txt">我的优惠券</span> <span class="bor"></span></div>
                        <!-- 空 -->
                        <div class="con-null" style="display: none">
                            <p>您还没有可使用的优惠券</p>
                        </div>


                        <div class="my-coupon-list">
                        </div>
                    </div>
                </div>

                <div class="check-coupon">
                    <a class="check-btn" href="javascript:toCoupon();">查看所有优惠券</a>
                </div>

            </div>
        </div>
    </div>
</div>
<script src="${webctx }js/jquery.fly.js"></script>
<script src="${ctx }/plugins/artTemplate/dist/template.js"></script>
<script type="text/html" id="user_center">
    <div class="user-msg-top">
        <a class="user-img fl" href="javascript:toManage();"><img src="{{filepath}}{{value.photo}}"></a>
        <div class="top-text fl">
            <p class="user-name" onclick="showusercenter();">{{value.loginname}}</p>
            <a class="sign-in" href="javascript:;">签到领积分</a>
        </div>
        <div class="clear"></div>
    </div>
    <div class="user-msg-bot">
        <div class="bot-item integral" onclick="toIntegral();">
            <p>{{value.integral}}</p>
            <p>积分</p>
        </div>
        <div class="bot-item coupon" onclick="toCoupon();">
            <p>{{value.couponnum}}</p>
            <p>优惠券</p>
        </div>
        <div class="bot-item news" onclick="toNews();">
            <p>{{value.newsnum}}</p>
            <p>消息</p>
        </div>
    </div>
</script>
<script type="text/html" id="couponHtml_available">
    <ul>
        {{each list as value i}}
        <li id="coupon{{value.id}}">
            <div class="coupon-text fl">
                <div class="text-price fl">￥<span>{{value.offerMoney}}</span></div>
                <div class="text-msg fl">
                    <p class="msg-p1">{{value.name}}</p>
                    <p class="msg-p2">满{{value.minMoney}}可使用</p>
                </div>
                <dic class="clear"></dic>
                <p class="coupon-time">
                    {{if value.remain<=3}}
                    快过期
                    {{else if value.active}}
                    {{value.remain}}天内有效
                    {{else}}
                    {{value.startDate | dateFormat:'yyyy/MM/dd hh:mm'}}-{{value.endDate | dateFormat:'yyyy/MM/dd hh:mm'}}
                    {{/if}}
                </p>
            </div>
            <a id="click{{value.id}}" class="get-coupon-btn fr" href="javascript:getCoupon('{{value.id}}');"><span
                    id="text{{value.id}}">立即领取</span></a>
        </li>
        {{/each}}
    </ul>
</script>
<script type="text/html" id="couponHtml_my">
    <ul>
        {{each list as value i}}
        <li>
            <div class="coupon-price fl">￥<span>{{value.offerMoney}}</span></div>
            <div class="coupon-text fl">
                <p class="msg-p1">{{value.name}}</p>
                <p class="msg-p2">满{{value.minMoney}}可使用</p>
                <p class="coupon-time">
                    {{if value.remain<=3}}
                    快过期
                    {{else}}
                    {{value.startDate | dateFormat:'yyyy/MM/dd'}}-{{value.endDate | dateFormat:'yyyy/MM/dd'}}
                    {{/if}}
                </p>
            </div>
            <div class="clear"></div>
        </li>
        {{/each}}
    </ul>
</script>
<script>
    $(function () {
        showCenter();
        showCoupons();
        showCartList();
        $w.httpRequest({
            url: "${ctx}/coupon/num",
            ok: function (ret) {
                $("#couponNum").text(ret.Data);
            }
        })
    });
    function showCenter() {
        if (islogin) {
            $w.httpRequest({
                url: ctx + "/user/center",
                ok: function (ret) {
                    $(".sid-user-msg").html(template("user_center", {
                        value: ret.Data,
                        filepath: getRandomFileserver()
                    }));
                }
            })
        }
    }
    function side_tologin() {
        window.location.href = ctx + "/user/showlogin";
        event.stopPropagation();
    }
    function toIntegral() {
        window.location.href = ctx + "/user/integral";
        event.stopPropagation();
    }
    function toCoupon() {
        window.location.href = ctx + "/coupon/show";
        event.stopPropagation();
    }
    function toNews() {
        window.location.href = ctx + "/user/stationnews";
        event.stopPropagation();
    }
    function toManage() {
        window.location.href = ctx + "/user/manage";
        event.stopPropagation();
    }
    function showusercenter() {
        window.location.href = ctx + "/user/home";
    }
    function showCoupons() {
        if (islogin) {
            $.ajax({
                type: "get",
                url: "${ctx}/coupon/avail",
                dataType: "json",
                success: function (ret) {
                    if (ret.MsgCode == '1') {
                        var data = ret.Data;
                        if (data.length > 0) {
                            showAvailableCoupons(data);
                        } else {
                            $(".get-coupon .con-null").show();
                        }
                    } else {
                        window.location.href = errorUrl;
                    }
                }
            });
            showReceivedCoupons();
        } else {
            $(".coupon-con").html('<div class="msg-txt">您尚未登录</div>');
        }
    }
    function showReceivedCoupons() {
        $.ajax({
            type: "get",
            url: "${ctx}/coupon",
            dataType: "json",
            success: function (ret) {
                if (ret.MsgCode == '1') {
                    var data = ret.Data;
                    if (data.length > 0) {
                        showMyCoupons(data);
                    } else {
                        $(".my-coupon .con-null").show();
                    }
                }
            }
        });
    }
    var get_coupon = false;
    function getCoupon(id) {
        if (get_coupon) {
            return false;
        }
        get_coupon = true;
        $w.httpRequest({
            url: ctx + "/coupon/receive/" + id,
            ok: function (ret) {
                if (!ret.Data) {
                    $("#text" + id).text("已领取");
                    $("#click" + id).attr("href", "javascript:void(0);");
                    hide_li($("#coupon" + id));
                }
                $(".msg-cart").html("领取成功");
                $(".msg-cart").show();
                $(".msg-cart").fadeOut("slow");
                showReceivedCoupons();
            },
            fail: function (ret) {
                $(".msg-cart").html(ret.MsgDesc);
                $(".msg-cart").show();
                $(".msg-cart").fadeOut("slow");
            },
            all: function () {
                get_coupon = false;
            }
        })
    }
    function hide_li(obj) {
        obj.animate({left: "270px"}, "slow", hide_li_sub); //偏移特效
        function hide_li_sub() { //li消失的函数
            obj.hide();
        }
    };
    function dateFormat(date, format) {
        if (typeof date === "string") {
            var mts = date.match(/(\/Date\((\d+)\)\/)/);
            if (mts && mts.length >= 3) {
                date = parseInt(mts[2]);
            }
        }
        date = new Date(parseInt(date));
        if (!date || date.toUTCString() == "Invalid Date") {
            return "";
        }

        var map = {
            "M": date.getMonth() + 1, //月份
            "d": date.getDate(), //日
            "h": date.getHours(), //小时
            "m": date.getMinutes(), //分
            "s": date.getSeconds(), //秒
            "q": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };


        format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
            var v = map[t];
            if (v !== undefined) {
                if (all.length > 1) {
                    v = '0' + v;
                    v = v.substr(v.length - 2);
                }
                return v;
            }
            else if (t === 'y') {
                return (date.getFullYear() + '').substr(4 - all.length);
            }
            return all;
        });
        return format;
    }
    function showAvailableCoupons(data) {
        template.helper('dateFormat', dateFormat);
        $(".get-coupon-list").html(template('couponHtml_available', {list: data}));
    }
    function showMyCoupons(data) {
        template.helper('dateFormat', dateFormat);
        $(".my-coupon-list").html(template('couponHtml_my', {list: data}));
    }
    function splice(data) {
        var type = data.type;
        var data = data.data;
        switch (type) {
            case 'list':
                var product = data.products[0];
                $("#sideCart" + product.cartId).html(showCommonProduct('', product));
                break;
            case 'complex':
                var products = data.products;
                for (var j = 0; j < products.length; j++) {
                    var product = products[j];
                    if (product.giftType == '') {
                        $("#sideCart" + product.cartId).html(showComplexProduct('', product));
                    }
                }
                break;
            case 'purchase_gift':
                $("#sideCart" + data.promotionId).html(showPurchaseGiftItem("", data));
                break;
            case 'money_off':
                $("#sideCart" + data.promotionId).html(showMoneyOffItem("", data));
                break;
        }
    }
    var synBuyCountCache = {};
    var synBuyCountTimer = {};
    function synBuyCount(type, id, cid, pid, count, price, flag) {
        var buycount = count + '';
        var buycountCache = synBuyCountCache[pid + ''] + '';
        if (buycount == buycountCache) {
            return true;
        } else {
            synBuyCountCache[pid + ''] = buycount;
            var synBuyCountTimerPre = synBuyCountTimer[pid + ''];
            if (synBuyCountTimerPre > 0) {
                window.clearTimeout(synBuyCountTimerPre);
                synBuyCountTimer[pid + ''] = -1;
            }
            var synBuyCountTimerTemp = window.setTimeout(function () {
                synBuyCountTimer[pid + ''] = -1;
                synCartItems(type, id, cid, pid, count, price, flag);
            }, 600);
            synBuyCountTimer[pid + ''] = synBuyCountTimerTemp;
        }
    }

    var submit = false;
    function synCartItems(type, id, cid, pid, count, price, flag) {
        if (submit)return;
        submit = true;
        $w.httpRequest({
            url: "${ctx}/order/syncart",
            data: {
                "promotionType": type,
                "promotionId": id,
                "id": cid,
                "productid": pid,
                "count": count,
                "price": price,
                "check": flag
            },
            ok: function (ret) {
                var data = ret.Data;
                splice(data);
                $("#sideCart_heji").text(data.price);
                //头部同步
                head_splice(data);
                $("#headCart_heji").text(data.price);
            },
            all: function () {
                submit = false;
            }
        })
    }
    function side_inputAmount(type, id, cid, pid, obj) {
        $(obj).val($(obj).val().replace(/[^0-9]/g, ''));
        if ($(obj).val() == '0' || $(obj).val() == '') $(obj).val('1');
        if ($(obj).attr("maxnum") && $(obj).val()) {
            var max = parseInt($(obj).attr("maxnum"));
            max = max > 0 ? max : 0;
            if (parseInt($(obj).val()) > max) $(obj).val(max);
        }

        var price = $("#sideCartprice" + cid).text();
        if (id) {
            price = $("#sideCartprice" + id).text();
        }
        synBuyCount(type, id, cid, pid, $(obj).val(), price, true);
    }
    function side_addamount(type, id, cid, pid, obj) {
        var txt = $("[name='buynumInp']", $(obj).parent());
        var n = parseInt(txt.val());
        n++;
        txt.val(n);
        side_inputAmount(type, id, cid, pid, txt);
    }
    function side_lessenamount(type, id, cid, pid, obj) {
        var txt = $("[name='buynumInp']", $(obj).parent());
        var n = parseInt(txt.val());
        n--;
        txt.val(n);
        side_inputAmount(type, id, cid, pid, txt);
    }
    function showCommonProduct(h, product) {
        if (product) {
            h += '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
                + '<div class="item-con">'
                + '<p class="name-specification">' + product.name + '&nbsp' + product.b_guige + '</p>'
                + '<p class="company">' + product.b_chanjia + '</p>'
                + ' <div class="price-count clear">'
                + '<em>￥<i id="sideCartprice' + product.cartId + '">' + product.totalPrice + '</i></em>'
                + '<a href="javascript:delCart(' + product.cartId + ');" class="delete-btn"></a>'
                + '<span class="count-action">'
                + '<i class="sub" onclick="side_lessenamount(' + "'list'" + ',null, ' + product.cartId + ', ' + product.id + ', this);">-</i>'
                + '<input name="buynumInp" type="text" onkeyup="side_inputAmount(' + "'list'" + ',null, ' + product.cartId + ', ' + product.id + ', this);" value="' + product.buyNum + '" maxnum="' + product.amount + '">'
                + '<i class="add" onclick="side_addamount(' + "'list'" + ',null, ' + product.cartId + ', ' + product.id + ', this);">+</i>'
                + '</span>'
                + '<span class="count">' + product.buyNum + '</span>'
                + '</div>'
                + '</div>';
        }
        return h;
    }
    function showCommonProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var product = items[i];
                h += '<div id="sideCart' + product.cartId + '" class="com-list-item cont-del-div clear">';
                h += showCommonProduct("", product);
                h += '</div>';
            }
        }
        return h;
    }
    function showBuySendProduct(h, product) {
        h += '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
            + '<div class="item-con">'
            + '<p class="name-specification">' + product.name + '&nbsp' + product.b_guige + '</p>'
            + '<p class="company">' + product.b_chanjia + '</p>'
            + ' <div class="price-count clear">'
            + '<em>￥' + product.totalPrice + '</em>'
            + '<a href="javascript:delCart(' + product.cartId + ');" class="delete-btn"></a>'
            + '<span class="count-action">'
            + '<i class="sub" onclick="side_lessenamount(' + "'complex',''," + product.cartId + ', ' + product.id + ', this);">-</i>'
            + '<input name="buynumInp" type="text" onkeyup="side_inputAmount(' + "'complex',''," + product.cartId + ', ' + product.id + ', this);" value="' + product.buyNum + '" maxnum="' + product.amount + '">'
            + '<i class="add" onclick="side_addamount(' + "'complex',''," + product.cartId + ', ' + product.id + ', this);">+</i>'
            + '</span>'
            + '<span class="count">' + product.buyNum + '</span>'
            + '</div>'
            + '</div>';
        if (product.buySend) {
            if (product.available) {
                h += '<p class="ms-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买送：' + product.name + '&nbsp' + product.b_guige + "×" + product.giftNum + '</a></p>';
            } else {
                h += '<p class="ms-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买' + product.standardNum + "送" + product.sendNum + '</a></p>';
            }
        }
        return h;
    }
    function showActiveProduct(h, type, product, promotionId) {
        h += '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
            + '<div class="item-con">'
            + '<p class="name-specification">' + product.name + '&nbsp' + product.b_guige + '</p>'
            + '<p class="company">' + product.b_chanjia + '</p>'
            + ' <div class="price-count clear">'
            + '<em>￥' + product.totalPrice + '</i></em>'
            + '<a href="javascript:delCart(' + product.cartId + ');" class="delete-btn"></a>'
            + '<span class="count-action">'
            + '<i class="sub" onclick="side_lessenamount(\'' + type + '\',' + promotionId + ', ' + product.cartId + ', ' + product.id + ', this);">-</i>'
            + '<input name="buynumInp" type="text" onkeyup="side_inputAmount(\'' + type + '\',' + promotionId + ', ' + product.cartId + ', ' + product.id + ', this);" value="' + product.buyNum + '" maxnum="' + product.amount + '">'
            + '<i class="add" onclick="side_addamount(\'' + type + '\',' + promotionId + ', ' + product.cartId + ', ' + product.id + ', this);">+</i>'
            + '</span>'
            + '<span class="count">' + product.buyNum + '</span>'
            + '</div>'
            + '</div>';
        if (product.buySend) {
            if (product.available) {
                h += '<p class="ms-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买送：' + product.name + '&nbsp' + product.b_guige + "×" + product.giftNum + '</a></p>';
            } else {
                h += '<p class="ms-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买' + product.standardNum + "送" + product.sendNum + '</a></p>';
            }
        }
        return h;
    }
    function showGiftProduct(h, product) {
        h += '<div class="com-list-item clear">'
            + '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
            + '<div class="item-con">'
            + '<p class="name-specification"><span>赠品</span>' + product.name + '&nbsp' + product.b_guige + '</p>'
            + '<p class="company">' + product.b_chanjia + '</p>'
            + '<div class="price-count clear">'
            + '<em>￥0.00</em>'
            + '<span style="text-align:center; margin-right:65px;">' + product.buyNum + '</span>'
            + '</div>'
            + '</div>'
            + '</div>';
        return h;
    }
    function showComplexProduct(h, product) {
        h += '<i id="sideCartprice' + product.cartId + '" style="display: none;">' + product.totalPrice + '</i>';
        h += showBuySendProduct("", product);
        return h;
    }
    function showComplexProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var product = items[i];
                if (product.giftType == '') {
                    h += '<div class="com-list-active cont-del-div clear"><div id="sideCart' + product.cartId + '" class="com-list-item clear">';
                    h += showComplexProduct("", product);
                    h += '</div></div>';
                }
            }
        }
        return h;
    }
    function showMoneyOffItem(h, item) {
        h += '<div class="active-title"><i id="sideCartprice' + item.promotionId + '" style="display: none">' + item.totalPrice + '</i><span>满减</span>';
        if (item.available) {
            h += " 活动已购满" + item.standardMoney;
            if (item.discountRate > 0) {
                h += ',已打<em>' + item.discountRate + '</em>折';
            } else {
                h += ',已减<em>' + item.discountMoney + '</em>元';
            }
        } else {
            h += "活动";
            var rules = item.items;
            for (var i = 0; i < rules.length; i++) {
                var rule = rules[i];
                h += "满" + rule.offMoney;
                if (rule.discountRate > 0) {
                    h += '打' + rule.discountRate + '折,';
                } else {
                    h += '减' + rule.discountMoney + ',';
                }
            }
        }
        h += '</div>';
        h += showActiveItem("", "money_off", item);
        return h;
    }
    function showMoneyOffProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                h += '<div id="sideCart' + item.promotionId + '" class="com-list-active cont-del-div clear">';
                h += showMoneyOffItem("", item);
                h += '</div>';
            }
        }
        return h;
    }
    function showActiveItem(h, type, item) {
        var products = item.products;
        for (var j = 0; j < products.length; j++) {
            var product = products[j];
            if (product.gift) {
                h += showGiftProduct("", product);
            } else if (product.giftType == '') {
                h += '<div class="com-list-item clear">';
                h += showActiveProduct("", type, product, item.promotionId);
                h += '</div>';
            }
        }
        return h;
    }
    function showPurchaseGiftItem(h, item) {
        h += '<div class="active-title"><em id="sideCartprice' + item.promotionId + '" style="display: none">' + item.totalPrice + '</em><span>满赠</span>';
        if (item.hasStandard) {
            if (item.standardMoney) {
                h += '活动已购满' + item.standardMoney + '元，已获得赠品<em>&nbsp;' + item.number + '&nbsp;</em>件';
            } else {
                h += '活动已购满' + item.standardAmount + '件，已获得赠品<em>&nbsp;' + item.number + '&nbsp;</em>件';
            }
        } else {
            if (item.standardMoney) {
                h += '活动购满' + item.standardMoney + '元，可获得赠品<em>&nbsp;' + item.number + '&nbsp;</em>件';
            } else {
                h += '活动购满' + item.standardAmount + '件，可获得赠品<em>&nbsp;' + item.number + '&nbsp;</em>件';
            }
        }
        h += '</div>';
        h += showActiveItem("", "purchase_gift", item);
        return h;
    }
    function showPurchaseGiftProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                h += '<div id="sideCart' + item.promotionId + '" class="com-list-active cont-del-div clear">';
                h += showPurchaseGiftItem("", item);
                h += '</div>';
            }
        }
        return h;
    }
    function showCartList() {
        $.ajax({
            type: "get",
            url: ctx + "/order/cart?_=" + new Date().toTimeString(),
            dataType: "json",
            success: function (ret) {
                if (ret.MsgCode == '-1') {
                    $(".log-out").show();
                } else if (ret.MsgCode == '1') {
                    var data = ret.Data;
                    $("#cartNum").text(data.count);
                    $("#cartNum").removeClass("homecount");
                    $("#couponNum").removeClass("homecount");
                    if (data.count > 0) {
                        $("#cart_content").empty();
                        var itemHtml = "";
                        itemHtml += '<div class="com-list">';
                        itemHtml += showCommonProducts("", data.list);
                        itemHtml += showComplexProducts("", data.complex);
                        itemHtml += showMoneyOffProducts("", data.money_off);
                        itemHtml += showPurchaseGiftProducts("", data.purchase_gift);
                        itemHtml += '</div><div class="go-cart">'
                            + '<p>已选 <em>' + data.count + '</em> 件, 共<i id="sideCart_heji">' + data.totalPrice + '</i>元</p>'
                            + '<a target="_blank" href="' + ctx + '/order/shopcart">去购物车结算</a>'
                            + '</div>';
                        $("#cart_content").html(itemHtml);
                        customScrollbar();
                    } else {
                        $(".com-null").show();
                    }
                }
            }
        })
    }
    function showCoupon() {
        if ($w.checkLogin(true, server + "/coupon/show")) {
            window.location.href = server + "/coupon/show";
        }
    }
    $(".count-action input").focus(function () {
        this.select();
    });

    //添加购物车
    function addCart(obj, event) {
        var img = $(obj).parents(".pricture-wrap").find('img').attr('src');
        var scorll_top = $(document).scrollTop();

        var offset = $('#carbox').offset(),
            flyer = $('<img class="u-flyer" src="' + img + '"/>');
        flyer.fly({
            start: {
                left: event.pageX,
                top: event.pageY - scorll_top
            },
            end: {
                left: offset.left,
                top: offset.top - scorll_top,
                width: 20,
                height: 20
            },
            onEnd: function () {
                flyer.fadeOut('slow', function () {
                    $(this).remove();
                })
            }
        });
    }

    $(".sid-cart").click(function () {
        if ($(this).hasClass("cur")) {
            $(this).removeClass("cur");
            $(".sidebar-con").removeClass("right");
        } else {
            $(this).addClass("cur");
            $(".sidebar-con").addClass("right");
        }
        $(".sid-company").removeClass("cur");
        $(".sidebar-commodity").show();
        $(".sidebar-coupon").hide();
    });
    $(".sid-company").click(function () {
        if ($(this).hasClass("cur")) {
            $(this).removeClass("cur");
            $(".sidebar-con").removeClass("right");
        } else {
            $(this).addClass("cur");
            $(".sidebar-con").addClass("right");
        }
        $(".sid-cart").removeClass("cur");
        $(".sidebar-commodity").hide();
        $(".sidebar-coupon").show();
    });
    $(".ico-close").click(function () {
        $(".sidebar-con").removeClass("right");
        $(".sid-cart").removeClass("cur");
        $(".sid-company").removeClass("cur");
    });

    $(".sid-stick").click(function () {
        var top_dis = $(document).scrollTop();
        top_dis > 0 ? $("html, body").animate({scrollTop: 0}, 500) : "";
    });

    $(document).bind('click', function (e) {

        var e = e || window.event; //浏览器兼容性

        var elem = e.target || e.srcElement;

        while (elem) { //循环判断至跟节点，防止点击的是div子元素
            if (( elem.id && elem.id == 'sidebar-con' ) || ( $(elem).attr('sid') == 'sid-cart-fly' )) {
                return;
            }
            elem = elem.parentNode;
        }

        $('#sidebar-con').removeClass("right"); //点击的不是div或其子元素
        $(".sid-cart").removeClass("cur");
    });

    function customScrollbar() {
        $("#cart_content").find(".com-list").mCustomScrollbar({
            mouseWheelPixels: 150
        });
    }
    $(".coupon-con").find(".coupon-list").mCustomScrollbar({
        mouseWheelPixels: 150
    });

    document.getElementById('sidebar-con').onmousewheel = function (event) {
        if (!event) event = window.event;
        this.scrollTop = this.scrollTop - (event.wheelDelta ? event.wheelDelta : -event.detail * 10);
        return false;
    }

</script>
<%@ include file="/website/BaiduBusiness.jsp" %>

